<?php
namespace Question\Controller;
use Common\Controller\Base;
class IndexController extends Base {
    //招聘模型对象
    private $Question = NULL;
    //应聘模型对象
    private $Candidates = NULL;
    protected function _initialize() {
        parent::_initialize();
        $this->Question = D('Question/Question');
        $this->Candidates = D('Question/Candidates');
    }
    public function ask(){
         if (IS_POST) {
             $post = I('post.', NULL, 'htmlspecialchars');
             if ($this->Candidates->addCandidates($post)) {
                 $this->success('你的疑问提交成功！正在等待审核后回复！', U('Index/index'));
             } else {
                 $error = $this->Candidates->getError();
                 $this->error($error ? $error : '提交失败！');
             }
         }else{
             $seo = seo('', "名师答疑", '师答疑关键词', '师答疑关描述'); 
             $this->assign('SEO', $seo);
             $this->display();
         }   
    }
    public function index() {
        $where['status']=array('eq','1');
        $where['is_back']=array('eq','1');
        $count = $this->Question->where($where)->count();
        $page = $this->page($count, 5);
        $data = $this->Question->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array( "id" => "DESC"))->select();
        foreach($data as &$val){
            switch($val['kemu']){
                case '1':
                    $val['kemu']="物理";
                    break;
                case '2':
                    $val['kemu']="化学";
                    break;
                case '3':
                    $val['kemu']="语文";
                    break;
                case '4':
                    $val['kemu']="数学";
                    break;
                case '5':
                    $val['kemu']="生物";
                    break;
                case '6':
                   $val['kemu']="地理";
                    break;
                case '7':
                    $val['kemu']="政治";
                    break;
                case '8':
                    $val['kemu']="历史";
                    break;
                default :
                   $val['kemu']="未知科目";
            }
        }
        $this->assign('list', $data);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->display();
    }

    //职位内页
    public function info() {
        $jobId = I('get.jobid', 0, 'intval');
        if (empty($jobId)) {
            $this->error('该招聘职位不存在！');
        }
        $info = $this->Job->getInfo($jobId);
        if (empty($info)) {
            $this->error('该招聘职位不存在！');
        }
        $seo = seo('', $info['zhiwei'], strip_tags($info['content']), $info['zhiwei']);

        $this->assign('SEO', $seo);
        $this->assign('info', $info);
        $this->display();
    }

    //应聘
    public function yingpin() {
        if (IS_POST) {
            $post = I('post.', NULL, 'htmlspecialchars');
            $post['username'] = service("Passport")->usename ? : "";
            $post['userid'] = service("Passport")->userid ? : 0;
            if ($this->Candidates->addCandidates($post)) {
                $this->success('提交成功！', U('Index/index'));
            } else {
                $error = $this->Candidates->getError();
                $this->error($error ? $error : '提交失败！');
            }
        } else {
            $jobId = I('get.jobid', 0, 'intval');
            if (empty($jobId)) {
                $this->error('该招聘职位不存在！');
            }
            $info = $this->Job->getInfo($jobId);
            if (empty($info)) {
                $this->error('该招聘职位不存在！');
            }
            //截止时间
            if ($info['enddate'] <= time()) {
                $this->error('该职位招聘已经结束！');
            }
            $seo = seo('', "应聘" . $info['zhiwei'], strip_tags($info['content']), $info['zhiwei']);

            $this->assign('SEO', $seo);
            $this->assign('info', $info);
            $this->assign('jobid', $jobId);
            $this->display();
        }
    }

}
